package com.mx.nbainformation.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

import com.mx.nbainformation.R;
import com.mx.nbainformation.Model.MatchData;
import com.mx.nbainformation.Model.PlayerData;
import com.mx.nbainformation.base.BaseActivity;
import com.mx.nbainformation.biz.BizPlayerScore;

public class ActivityPlayerScore extends BaseActivity {
	private String group = "group";
	private String targetUrl = "";

	private SimpleExpandableListAdapter _adapter;
	private ExpandableListView _lvPlayer;
	private List<List<Map<String, String>>> _list;
	private List<Map<String, String>> _group;

	private BizPlayerScore _biz;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_player_score);
		targetUrl = getIntent().getStringExtra(MatchData.TARGETURL);
		initClass();
		initControl();
		initListView();
		TitleButtonOfOne(false);
		addZhuamob();
		new DataAsyncTask().execute();
	}

	private void initClass() {
		_biz = new BizPlayerScore(this);
	}

	private void initControl() {
		_lvPlayer = (ExpandableListView) findViewById(R.id.listPlayer);
	}

	private void initListView() {
		_list = new ArrayList<List<Map<String, String>>>();
		_group = new ArrayList<Map<String, String>>();
		_adapter = new SimpleExpandableListAdapter(this, _group,
				R.layout.item_group_name, new String[] { group },
				new int[] { R.id.txt_group_name }, _list,
				R.layout.item_player_score, new String[] {
						PlayerData.PlayerName, PlayerData.PlayerTime,
						PlayerData.PlayerShot, PlayerData.PlayerThree,
						PlayerData.PlayerFQ, PlayerData.PlayerFront,
						PlayerData.PlayerBehind, PlayerData.PlayerAll,
						PlayerData.PlayerZG, PlayerData.PlayerQD,
						PlayerData.PlayerGM, PlayerData.PlayerSW,
						PlayerData.PlayerFG, PlayerData.PlayerDF }, new int[] {
						R.id.item_player_name, R.id.item_player_time,
						R.id.item_player_shot, R.id.item_player_three,
						R.id.item_player_punish,
						R.id.item_player_frontBackboard,
						R.id.item_player_behindBackboard,
						R.id.item_player_allBackboard, R.id.item_player_assist,
						R.id.item_player_qd, R.id.item_player_gm,
						R.id.item_player_sw, R.id.item_player_fg,
						R.id.item_player_df });
		_lvPlayer.setAdapter(_adapter);
	}

	class DataAsyncTask extends AsyncTask<String, Void, List<String>> {

		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			showProgressDialog(getResources().getString(R.string.LoadingTitle),getResources().getString(R.string.LoadingContext), null);
		}

		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub
			return _biz.getListName(targetUrl);
		}

		@Override
		protected void onPostExecute(List<String> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mAlertDialog.dismiss();
			if (result == null)
				return;
			Map<String, String> map;
			for (String temp : result) {
				map = new HashMap<String, String>();
				map.put(group, temp);
				_group.add(map);
			}
			List<List<Map<String, String>>> list = _biz.getPlayerList(targetUrl);
			_list.addAll(list);
			_adapter.notifyDataSetChanged();
			_lvPlayer.expandGroup(0);
			_lvPlayer.expandGroup(1);
		}
	}

}
